/** 京东种豆, 有自定义参数签名，暂没破它 */
let {getForJson, postForJson} = require('./jdClient.js');

/** 收集营养素 */
function receiveNutrients() {
    let curTime = new Date().getTime();
    let url = "https://api.m.jd.com/client.action?functionId=receiveNutrients&clientVersion=9.0.4&build=73764&client=android&d_brand=Xiaomi&d_model=MI6&osVersion=9&screen=1920*1080&partner=tencent&aid=a1893ed6f4dbc682&oaid=9646de4473ddf7cc&eid=eidAd74d812162s3+6nygm9TRJaswEPkVpLhQfup8/P1yxlN19lmwg+AEeW3OWl4XDjLPHdRNhUKVFG/3Y4B2OyHQzM8ggdUcBQFDA5zYZJcs3hP+MZX&sdkVersion=28&lang=zh_CN&uuid=865441031402480-4c49e351c106&area=19_1601_3633_63252&networkType=wifi&wifiBssid=unknown&uts=0f31TVRjBSsqndu4%2FjgUPz6uymy50MQJgp4QLLfMtLHefEnMPpRkmdfX5ulDs4DIRMC%2BZ7lxrMDiFPBW%2FQKdLqJQIiFmsOiMlqgompUQrJ6nmjnwvmgiXy5obX0lTnsnjtO7VERExS4seWa2MNJkBObcZwfUsLx6Cx%2B9V2DGEv5XFSN6IwZwevytvy2iNAZNlLDFT%2BU%2BLGDtQPdRYK83ZQ%3D%3D&sign=7a5f923a028be0eec744229e384d5258&sv=100&st=" + curTime;
    let data = {
        "monitor_refer": "plant_receiveNutrients",
        "monitor_source": "plant_app_plant_index",
        "roundId": "pxzhyradledygoqbns6eertieu",
        "version": "9.0.0.1"
    };
    let reqData = "body=" + encodeURIComponent(JSON.stringify(data));
    let res = postForJson(url, reqData);
    if (res.code == 0 && res.data) {
        console.log('收集营养素%s滴', res.data.nutrients);
    } else {
        console.log('收集营养素失败:' + JSON.stringify(res));
    }
}

/** 查询收集好友营养素记录 */
function collectHistoryByFriend() {
    let curTime = new Date().getTime();
    let url = "https://api.m.jd.com/client.action?functionId=collectHistoryByFriend&clientVersion=9.0.4&build=73764&client=android&d_brand=Xiaomi&d_model=MI6&osVersion=9&screen=1920*1080&partner=tencent&aid=a1893ed6f4dbc682&oaid=9646de4473ddf7cc&eid=eidAd74d812162s3+6nygm9TRJaswEPkVpLhQfup8/P1yxlN19lmwg+AEeW3OWl4XDjLPHdRNhUKVFG/3Y4B2OyHQzM8ggdUcBQFDA5zYZJcs3hP+MZX&sdkVersion=28&lang=zh_CN&uuid=865441031402480-4c49e351c106&area=19_1601_3633_63252&networkType=wifi&wifiBssid=unknown&uts=0f31TVRjBSsqndu4%2FjgUPz6uymy50MQJgp4QLLfMtLHefEnMPpRkmdfX5ulDs4DIRMC%2BZ7lxrMDiFPBW%2FQKdLqJQIiFmsOiMlqgompUQrJ6nmjnwvmgiXy5obX0lTnsnjtO7VERExS4seWa2MNJkBObcZwfUsLx6Cx%2B9V2DGEv5XFSN6IwZwevytvy2iNAZNlLDFT%2BU%2BLGDtQPdRYK83ZQ%3D%3D&sign=dca6dbd9b8b8c58f743927c102d0de6a&sv=101&st=" + curTime;
    let data = {
        "monitor_refer": "collectHistoryByFriend",
        "monitor_source": "plant_app_plant_index",
        "version": "9.0.0.1"
    };
    let reqData = "body=" + encodeURIComponent(JSON.stringify(data));
    let res = postForJson(url, reqData);
    if (res.code == 0 && res.data) {
        // res.data 格式
        // res.data.desc=收取营养液1瓶
        // res.data.friendCollectId=1593483725181
        // res.data.friendNickName=jd_MkhjRwQVVVJt
        // res.data.friendParadiseUuid=bqvfokpr4ourbzswcinq76i5djcymr56qbdfsvi
        console.log('查询收集好友营养素记录：' + JSON.stringify(res));
    } else {
        console.log('查询收集好友营养素记录失败:' + JSON.stringify(res));
    }

}

/** 获取好友列表 */
function planFriendList() {
    let curTime = new Date().getTime();
    let url = "https://api.m.jd.com/client.action?functionId=plantFriendList&clientVersion=9.0.4&build=73764&client=android&d_brand=Xiaomi&d_model=MI6&osVersion=9&screen=1920*1080&partner=tencent&aid=a1893ed6f4dbc682&oaid=9646de4473ddf7cc&eid=eidAd74d812162s3+6nygm9TRJaswEPkVpLhQfup8/P1yxlN19lmwg+AEeW3OWl4XDjLPHdRNhUKVFG/3Y4B2OyHQzM8ggdUcBQFDA5zYZJcs3hP+MZX&sdkVersion=28&lang=zh_CN&uuid=865441031402480-4c49e351c106&area=19_1601_3633_63252&networkType=wifi&wifiBssid=unknown&uts=0f31TVRjBSsqndu4%2FjgUPz6uymy50MQJgp4QLLfMtLHefEnMPpRkmdfX5ulDs4DIRMC%2BZ7lxrMDiFPBW%2FQKdLqJQIiFmsOiMlqgompUQrJ6nmjnwvmgiXy5obX0lTnsnjtO7VERExS4seWa2MNJkBObcZwfUsLx6Cx%2B9V2DGEv5XFSN6IwZwevytvy2iNAZNlLDFT%2BU%2BLGDtQPdRYK83ZQ%3D%3D&sign=21e82045a798f2f90ac556c3681c4153&sv=101&st=" + curTime;
    let data = {
        "monitor_refer": "plantFriendList",
        "monitor_source": "plant_app_plant_index",
        "pageNum": "1",
        "version": "9.0.0.1"
    };
    let reqData = "body=" + encodeURIComponent(JSON.stringify(data));
    let res = postForJson(url, reqData);
    if (res.code == 0 && res.data && res.data.friendInfoList) {
        // 格式： .nutrCount 营养素数量
        return res.data.friendInfoList;
    } else {
        console.log('获取好友列表失败:' + JSON.stringify(res));
    }
}

/** 收集好友营养素 */
function collectUserNutr() {
    let curTime = new Date().getTime();
    let url = "https://api.m.jd.com/client.action?functionId=collectUserNutr&clientVersion=9.0.4&build=73764&client=android&d_brand=Xiaomi&d_model=MI6&osVersion=9&screen=1920*1080&partner=tencent&aid=a1893ed6f4dbc682&oaid=9646de4473ddf7cc&eid=eidAd74d812162s3+6nygm9TRJaswEPkVpLhQfup8/P1yxlN19lmwg+AEeW3OWl4XDjLPHdRNhUKVFG/3Y4B2OyHQzM8ggdUcBQFDA5zYZJcs3hP+MZX&sdkVersion=28&lang=zh_CN&uuid=865441031402480-4c49e351c106&area=19_1601_3633_63252&networkType=wifi&wifiBssid=unknown&uts=0f31TVRjBSsqndu4%2FjgUPz6uymy50MQJgp4QLLfMtLHefEnMPpRkmdfX5ulDs4DIRMC%2BZ7lxrMDiFPBW%2FQKdLqJQIiFmsOiMlqgompUQrJ6nmjnwvmgiXy5obX0lTnsnjtO7VERExS4seWa2MNJkBObcZwfUsLx6Cx%2B9V2DGEv5XFSN6IwZwevytvy2iNAZNlLDFT%2BU%2BLGDtQPdRYK83ZQ%3D%3D&sign=6794304d3ddb703250832cca54b96508&sv=101&st=" + curTime;
    let data = {
        "monitor_refer": "collectUserNutr",
        "monitor_source": "plant_app_plant_index",
        "paradiseUuid": "kxlh75ou5iuynikfmyujgsz2va",
        "roundId": "pxzhyradledygoqbns6eertieu",
        "version": "9.0.0.1"
    };
    let reqData = "body=" + encodeURIComponent(JSON.stringify(data));
    let res = postForJson(url, reqData);
    if (res.code == 0 && res.data) {
        console.log('收取好友营养素%s滴', res.data.friendNutrRewards)
    } else {
        console.log('收取好友营养素失败:' + JSON.stringify(res));
    }
}

function getWater() {
    let url = "https://api.m.jd.com/?functionId=uvReport&d_brand=Xiaomi&screen=1920*1080&clientVersion=5.4.40&uuid=04b16f55-8db3-3ccc-97af-dbb5b6eec635&d_model=MI6&pin=jd_494844e91c166&osVersion=9&partner=xiaomi&t=1593267480782&build=666&appid=yingyan&client=android&sdkVersion=28&networkType=WiFi&sign=307a18cca532816d34289630035844b41c729bb3f7ee458fe6cdba1c07c3e4a7";
    let data = {"msg":[{"appId":"791e55183cf7f399f67cf93fa993961a","clientVersion":"5.4.40","buildCode":"666","uuid":"04b16f55-8db3-3ccc-97af-dbb5b6eec635"}]};
    data = "body=" + encodeURIComponent(data);
    data = "body=%7B%22msg%22%3A%5B%7B%z%22%3A%22791e55183cf7f399f67cf93fa993961a%22%2C%22clientVersion%22%3A%225.4.40%22%2C%22buildCode%22%3A%22666%22%2C%22uuid%22%3A%2204b16f55-8db3-3ccc-97af-dbb5b6eec635%22%7D%5D%7D";
    let response = postForJson(url, data);
    // console.log(JSON.stringify(response));
    if (response.code == 0) {
        console.log('定时领水滴：获得%s滴', response.amount);
    } else {
        console.log('定时领水滴失败: ' + JSON.stringify(response));
    }
}

function run() {
    receiveNutrients();
    collectHistoryByFriend();
    planFriendList();
    collectUserNutr();
    getWater();
}

run()
// module.exports = {
//     run: run
// }
